//
//  StatusCell.swift
//  我的微博
//
//  Created by ZhangChao on 15/9/8.
//  Copyright © 2015年 ZhangChao. All rights reserved.
//

import UIKit
// 控件间的间距
let statusCellMargin : CGFloat = 12
// 头像的宽高
let statusCellWidth : CGFloat = 35
// 默认的图片大小
let statusPictureItemWidth : CGFloat = 90
// 默认的图片间距
let statusPictureItemMargin : CGFloat = 10
// 每行图片的最大数量
let statusPictureMaxCount : CGFloat = 3
// 配图视图的最大尺寸
let statusPictureMaxWidth : CGFloat = statusPictureItemWidth * statusPictureMaxCount + statusPictureItemMargin * (statusPictureMaxCount - 1)

class StatusCell: UITableViewCell {

    // 视图模型
    var statusViewModel : StatusViewModel?{
        didSet{
            
            // 顶部视图赋值
            topView.statusViewModel = statusViewModel
            
            contentLabel.text = statusViewModel?.status.text
            
            // 图片模型赋值
            pictureView.statusViewModel = statusViewModel
            
            // 配图的大小
            pictureViewWidthCons?.constant = pictureView.bounds.width
            pictureViewHeightCons?.constant = pictureView.bounds.height
            // 判断有没有图片,设置顶部视图间距
            pictureViewTopCons?.constant = statusViewModel?.thumbnailUrl?.count == 0 ? 0 : statusCellMargin
        }
    }
    
    // 定义属性,记录图片的约束
    var pictureViewWidthCons : NSLayoutConstraint?
    
    var pictureViewHeightCons : NSLayoutConstraint?
    
    var pictureViewTopCons : NSLayoutConstraint?
    // 设置行高
    func rowHeight(viewModel: StatusViewModel) -> CGFloat {
        // 1.设置视图模型, 会调用视图模型的didSet方法
        statusViewModel = viewModel
        
        // 2.有了内容之后更新约束
        layoutIfNeeded()
        
        // 3.返回底部视图的做大Y值
        return CGRectGetMaxY(bottomView.frame)
        
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
        
    }
    
    func setupUI(){
        
        backgroundColor = UIColor.whiteColor()
        
        let separateView = UIView()
        separateView.backgroundColor = UIColor(white: 0.85, alpha: 1)
        
        // 添加控件
        contentView.addSubview(separateView)
        contentView.addSubview(topView)
        contentView.addSubview(contentLabel)
        contentView.addSubview(pictureView)
        contentView.addSubview(bottomView)
        
        // 这里设置的宽度要取屏幕的宽度,不能直接取cell得宽度,cell的宽度是320 (iphone4的遗留问题)
        let width = UIScreen.mainScreen().bounds.width
        
        // 1.顶部分割视图
        separateView.ff_AlignInner(type: ff_AlignType.TopLeft, referView: contentView, size: CGSize(width: width, height: 10))
        
        // 2.顶部视图
        topView.ff_AlignInner(type: ff_AlignType.TopLeft, referView: separateView, size: CGSize(width: width, height: statusCellWidth + 2 * statusCellMargin),offset: CGPoint(x: 0, y: 10))
        
        // 3.内容
        contentLabel.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: topView, size: nil, offset: CGPoint(x: statusCellMargin, y: statusCellMargin))
        
//        // 4.图片
//        let cons = pictureView.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: contentLabel, size: CGSize(width: statusPictureMaxWidth, height: statusPictureMaxWidth), offset: CGPoint(x: 0, y: statusCellMargin))
//        // 记录约束
//        pictureViewWidthCons = pictureView.ff_Constraint(cons, attribute: NSLayoutAttribute.Width)
//        pictureViewHeightCons = pictureView.ff_Constraint(cons, attribute: NSLayoutAttribute.Height)
//        pictureViewTopCons = pictureView.ff_Constraint(cons, attribute: NSLayoutAttribute.Top)
        
        // 5.底部视图
        bottomView.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: pictureView, size: CGSize(width: width, height: 44), offset: CGPoint(x: -statusCellMargin, y: statusCellMargin))
        
        // 不使用自动行高的时候,最好不要设置和底部视图的关系
        // bottomView.ff_AlignInner(type: ff_AlignType.BottomRight, referView: contentView, size: nil)
        
    }
    
    private lazy var topView : StatusCellTopView = StatusCellTopView()
    
    lazy var contentLabel : UILabel = UILabel(title: nil,color: UIColor.darkGrayColor(), fontSize: 15,layoutWidth : UIScreen.mainScreen().bounds.width - 2 * statusCellMargin)
    
    lazy var pictureView : StatusPictureView = StatusPictureView()
    
    lazy var bottomView : StatusCellBottomView = StatusCellBottomView()
    
}
